<?php
/**
 * @package: Asalsuri
 * @subpackage: Personal Model
 * @author: Pham Van Diep
 * @date: 27/2/2014
 * 
*/
class Personal_Model extends CI_Model{
	private $_database;
	public function __construct() {
        parent::__construct();
		$this->_database = $this->db->conn_id;
		
	}
    /**
     * @author: Pham Van Diep
     * @description: create object personal
     * @param array $row: array personal 
     * @return object | null
    */
	private function _PersonalToObj($row) {
		if(isset($row)) {
			$objPersonal			     = new stdClass();
			$objPersonal->personalid 	 = isset($row['personalid'])    ? $row['personalid']   : 0;
			$objPersonal->firstname      = isset($row['firstname'])     ? $row['firstname']    : '';
			$objPersonal->lastname       = isset($row['lastname'])      ? $row['lastname']     : '';
			$objPersonal->fullname       = isset($row['fullname'])      ? $row['fullname']     : '';
            $objPersonal->nickname       = isset($row['nickname'])      ? $row['nickname']     : '';
            $objPersonal->birthdate      = isset($row['birthdate'])     ? CI_Function::datevn($row['birthdate']): '';
            $objPersonal->place_of_date  = isset($row['place_of_date']) ? $row['place_of_date']: '';
            $objPersonal->avatar         = isset($row['avatar'])        ? $row['avatar']       : '';
			$objPersonal->tag 	         = isset($row['tag'])           ? $row['tag']          : '';
			$objPersonal->short 	     = isset($row['short'])         ? $row['short']        : '';
			$objPersonal->description	 = isset($row['description'])   ? $row['description']  : '';
           
            $objPersonal->created        = isset($row['created'])       ? CI_Function::datevn($row['created']): '';
			$objPersonal->modified       = isset($row['modified'])      ? CI_Function::datevn($row['modified']): '';
            $objPersonal->status         = isset($row['status'])        ? $row['status']       : -1;
			$objPersonal->type           = isset($row['type'])          ? $row['type']         : -1;
			$objPersonal->features       = isset($row['features'])      ? $row['features']     : '';
			
            $objPersonal->logid         = isset($row['logid'])       ? $row['logid']   : 0;          
			return $objPersonal;
		}
		return null;
	}
    /**
     * @author: Pham Van Diep
     * @description: get Personal info
     * @param integer $intId: ID
     * @return object | null
    */
	
	public function get($intId) {	
		$stmt = $this->_database->prepare('CALL personals_get(?)');
		$stmt->bindParam(1, $intId, PDO::PARAM_INT, 11);
		$stmt->execute();
		return  $this->_PersonalToObj($stmt->fetch());
        //return $stmt->fetchAll();			
	}
    /**
    * @author: Pham Van Diep
    * @description: get all Personals
    * @param integer $intLoginid: user login id
    * @param string $strName: name of Personal
    * @param string $strNickname: nickname of Personal
    * @param string $strCountry: name country
    * @param string $strLogo: path of logo image 
    * @param string $strSlug: slug
    * @param string $strShort: string short description
    * @param string $strDescription: string description
    * @param string $strTag: string tag  
    * @param array
    */
    public function add($intPloginid ,$strFirstName , $strLastName, $strNickname,$strBirthDay ,$strPlace, $strAvatar, $strTag, $strShort, $strDescription, $intType,$strFeatures) {
		$stmt = $this->_database->prepare('CALL personals_add(?,?,?,?,?,?,?,?,?,?,?,?)');
		$stmt->bindParam(1, $intPloginid,         PDO::PARAM_INT, 11);
		$stmt->bindParam(2, $strFirstName,        PDO::PARAM_STR, 50);
		$stmt->bindParam(3, $strLastName,         PDO::PARAM_STR, 50);
		$stmt->bindParam(4, $strNickname,         PDO::PARAM_STR, 100);
		$stmt->bindParam(5, $strBirthDay,         PDO::PARAM_STR);
		$stmt->bindParam(6, $strPlace,            PDO::PARAM_STR, 2);
		$stmt->bindParam(7, $strAvatar,           PDO::PARAM_STR, 255);
		$stmt->bindParam(8, $strTag,              PDO::PARAM_STR, 100);	       
        $stmt->bindParam(9, $strShort,            PDO::PARAM_STR, 255);
        $stmt->bindParam(10, $strDescription,     PDO::PARAM_STR);
		$stmt->bindParam(11, $intType,            PDO::PARAM_INT, 4);    
		$stmt->bindParam(12, $strFeatures,        PDO::PARAM_STR);       				
		$stmt->execute();	
        $result = $stmt->fetch();
	
        return $this->_getErrorCode($result);	
	}
    /**
    * @author: Pham Van Diep
    * @description: get all Personals
    * @param integer $intLoginid: user login id
    * @param integer $intStatus: status
    * @param integer $intOption: option : 0-> all
    * @param string $strKeyword 
    * @param datetime $dStart : start date
    * @param datetime $dEnd : end date 
    * @param integer  $intOffset: start of paging
    * @param integer  $intLimit: paging number 
    * @param array
    */	
    public function getAll($intLoginid,$intStatus,$intOption,$strKeyword,$dStart,$dEnd,$intOffset,$intLimit ,&$intTotal) {
		$stmt = $this->_database->prepare('CALL personals_getall(?,?,?,?,?,?,?,?)');
		$stmt->bindParam(1, $intLoginid,     PDO::PARAM_INT);
		$stmt->bindParam(2, $intStatus,      PDO::PARAM_INT);
		$stmt->bindParam(3, $intOption,      PDO::PARAM_INT, 4);
        $stmt->bindParam(4, $strKeyword,     PDO::PARAM_STR, 50);
        $stmt->bindParam(5, $dStart,         PDO::PARAM_STR);
        $stmt->bindParam(6, $dEnd,           PDO::PARAM_STR);
        $stmt->bindParam(7, $intOffset,      PDO::PARAM_INT);
        $stmt->bindParam(8, $intLimit,       PDO::PARAM_INT, 6);	
		$stmt->execute();
		$result = $stmt->fetchAll();        
		$intTotal = isset($result[0]['foundRows']) ? $result[0]['foundRows'] : 0;
		return isset($result) ? $result : null;
	}
    /**
    * @author: Pham Van Diep
    * @description: delete Personal
    * @param integer $intLoginid: user login id
    * @param integer $intPersonalid: Personal id     
    * @param integer
    */
    public function delete($intLoginid,$intPersonalid){
        $stmt = $this->_database->prepare('CALL personals_delete(?,?)');
		$stmt->bindParam(1, $intLoginid, PDO::PARAM_INT, 11);
        $stmt->bindParam(2, $intPersonalid,  PDO::PARAM_INT, 11);
		$stmt->execute();
        $result = $stmt->fetch();
        return $this->_getErrorCode($result);			
    }
    /**
    * @author: Pham Van Diep
    * @description: update Personal infomation
    * @param integer $intLoginid: user login id
    * @param string $strName: name of Personal
    * @param string $strNickname: nickname of Personal
    * @param string $strCountry: name country
    * @param string $strLogo: path of logo image 
    * @param string $strSlug: slug
    * @param string $strShort: string short description
    * @param string $strDescription: string description
    * @param string $strTag: string tag  
    * @param integer
    */
	
    public function update($intPloginid,$intPersonalid, $strFirstName , $strLastName, $strNickname,$strBirthDay, $strPlace, $strAvatar, $strTag, $strPshort, $strPdescription, $intType) {
		$stmt = $this->_database->prepare('CALL personals_update(?,?,?,?,?,?,?,?,?,?,?,?)');
		$stmt->bindParam(1, $intPloginid,         PDO::PARAM_INT, 11);
        $stmt->bindParam(2, $intPersonalid,       PDO::PARAM_INT, 11);
		$stmt->bindParam(3, $strFirstName,        PDO::PARAM_STR, 50);
		$stmt->bindParam(4, $strLastName,         PDO::PARAM_STR, 50);
		$stmt->bindParam(5, $strNickname,         PDO::PARAM_STR, 100);
		$stmt->bindParam(6, $strBirthDay,         PDO::PARAM_STR);
		$stmt->bindParam(7, $strPlace,            PDO::PARAM_STR, 2);
		$stmt->bindParam(8, $strAvatar,           PDO::PARAM_STR, 255);
		$stmt->bindParam(9, $strPtag,             PDO::PARAM_STR, 100);	       
        $stmt->bindParam(10, $strPshort,          PDO::PARAM_STR, 255);
        $stmt->bindParam(11, $strPdescription,    PDO::PARAM_STR);
		$stmt->bindParam(12, $intType,            PDO::PARAM_INT, 4);       				
		$stmt->execute();	
        $result = $stmt->fetch();
        return $this->_getErrorCode($result);	
	}
    /**
    * @author: Pham Van Diep
    * @description: update status of Personal
    * @param integer $intLoginid: user login id
    * @param integer $intPersonalid: Personal id
    * @param integer $intStatus: Personal status
    * @param integer $intPerm: permission      
    * @param integer
    */
	
    public function updateStatus($intLoginid,$intPersonalid,$intStatus,$intPerm){
        $stmt = $this->_database->prepare('CALL personals_update_status(?,?,?,?)');
		$stmt->bindParam(1, $intLoginid,     PDO::PARAM_INT, 11);
        $stmt->bindParam(2, $intPersonalid,  PDO::PARAM_INT, 11);
		$stmt->bindParam(3, $intStatus,      PDO::PARAM_INT, 4);
		$stmt->bindParam(4, $intPerm,        PDO::PARAM_INT, 4);							
		$stmt->execute();	
        $result = $stmt->fetch();
        return $this->_getErrorCode($result);	
    }	
	/**
	*	@description: get Error code from query result
	*	@param: mixed query result
	*	@return: int
	*/
	private function _getErrorCode ( $tableData ) {
		return isset($tableData['errCode']) ? $tableData['errCode'] : -1;
	}	
	
}